package org.bjf.modules.sys.enums;

import org.apache.commons.lang3.StringUtils;

/**
 * CRUD权限方法归类
 *
 * @author bjf
 */
public enum PermissionMethod {

  CREATE(3, "add,save,create,insert"),
  READ(2, "get,list,query,search,check"),
  UPDATE(1, "update,edit,modify,do"),
  DELETE(0, "del,remove");

  /**
   * 权限位置
   */
  private int pos;
  /**
   * 支持的方法
   */
  private String method;

  PermissionMethod(int pos, String method) {
    this.pos = pos;
    this.method = method;
  }

  /**
   * 根据方法名判断类型
   */
  public static PermissionMethod get(String methodName) {
    for (PermissionMethod method : PermissionMethod.values()) {
      String methodSupport = method.getMethod();
      String[] arry = StringUtils.split(methodSupport, ",");
      for (String support : arry) {
        if (methodName.startsWith(support)) {
          return method;
        }
      }
    }
    // 不匹配，则定义为update权限
    return PermissionMethod.UPDATE;
  }

  public int getPos() {
    return pos;
  }

  public String getMethod() {
    return method;
  }

}
